<?php

namespace App\Imports;


use App\Models\airline\base\CodeDetailModel;
use App\Models\airline\base\CodeModel;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use stdClass;
use App\Models\DeptModel;
use App\Models\AdminRoleModel;

class ContainerImport implements ToCollection, WithMultipleSheets
{


    private $result;

    public function __construct() {
        $this->result = new stdClass;
    }

    public function collection(Collection $rows){
        $lines = [];
        $this->result->error = [];


        foreach ($rows as $k => $row) {
            if($k < 1) continue;

            if($row[0] == ''){
                $this->result->error[] = '第'.$k.'行，集装器ID不能为空';
                continue;
            }

            $code = $row[1];
            $vehicle = $row[2];
            $code_id = 0;
            if($code != ''){
                $find_code = CodeModel::where('code', $code)->first();
                if(!$find_code){
                    $this->result->error[] = '第'.$k.'行，CODE不存在';
                    continue;
                }else{
                    $code_id = $find_code->id;

                    if($vehicle != ''){
                        $find_code_detail = CodeDetailModel::where('code_id', $code_id)->where('name', $vehicle)->first();
                        if(!$find_code_detail){
                            $this->result->error[] = '第'.$k.'行，载具不在CODE下不存在';
                        }
                    }
                }
            }



            $lines[] = [
                'container_id' => $row[0],
                'code_id' => $code_id,
                'vehicle' => $vehicle,
            ];
        }

        $this->result->lines = $lines;
    }

    public function sheets(): array {
        return [ 0 => $this];
    }

    public function get_result(){
        return $this->result;
    }


}
